package com.ruslan.magaz.service;


import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruslan.magaz.entity.User;
import com.ruslan.magaz.repository.UserRepository;

@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Autowired
    private  UserRepository userRepository;


	@Override
	public Page<User> getUsers(int page, Integer maxResults) {
		PageRequest request = new PageRequest(page, maxResults, Sort.Direction.ASC, "firstName");
        return userRepository.findAll(request);
	}


	@Override
	public void addUser(User user) {
		userRepository.saveAndFlush(user);
		
	}


	@Override
	public void removeUser(int id) {
		userRepository.delete(id);
        userRepository.flush();
		
	}


	@Override
	public User getUserById(int id) {
		return userRepository.findOne(id);
	}



	@Override
	public User updateUser(User user) {
		return userRepository.saveAndFlush(user);
	}


	@Override
	public List<String> getFirstLetters() {
		return userRepository.getFirstLetters();
	}


	@Override
	public Page<User> getUsersForLetter(String letter, int page,
			Integer maxResults) {
		PageRequest request = new PageRequest(page, maxResults, Sort.Direction.ASC, "firstName");
        return userRepository.findAllForLetter(letter, request);
	}


	@Override
	public int countSameUser(String firstName, String middleName,
			String lastName) {
		return userRepository.checkSameUser(firstName, middleName, lastName);
	}


	@Override
	public int countIdenticalUsers(int id, String firstName, String middleName,
			String lastName) {
		return userRepository.countIdenticalUsers(id, firstName, middleName, lastName);
	}



}


